package com.runfast.waimai.web.controller.data;

import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.common.web.entity.ResultCode;
import com.runfast.waimai.dao.mapper.RunfastDataCenterMapper;
import com.runfast.waimai.service.RunfastBusinessDailyService;
import com.runfast.waimai.dao.model.BaseUser;
import com.runfast.waimai.dao.model.WaiMaiRunfastAgentaccounts;
import com.runfast.waimai.utils.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by WQ on 2018/6/5.
 */
@RestController
@RequestMapping("/server/data/")
public class DataController {
    @Autowired
    private RunfastBusinessDailyService businessDailyService;
    @Autowired
    private RunfastDataCenterMapper dataCenterMapper;

    @GetMapping("index")
    public Result index(Integer id) {
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts = SessionContext.getCurrentRunfastAgentaccounts();
        if (admin == null && agentAccounts == null) {
            return Result.fail(ResultCode.ACCOUNT_RELOGIN);
        }
        if (agentAccounts != null) {
            id = agentAccounts.getAgentid();
        }
        Calendar calendar = Calendar.getInstance();
        boolean isFirstDayOfMonth = false;
        if (calendar.get(Calendar.DATE) == 1)
            isFirstDayOfMonth = true;
        Calendar calendar2 = Calendar.getInstance();
        calendar2.set(Calendar.HOUR_OF_DAY, 0);
        calendar2.set(Calendar.MINUTE, 0);
        calendar2.set(Calendar.SECOND, 0);
        Date now = calendar.getTime();
        Date now0 = calendar2.getTime();
        calendar.add(Calendar.DATE, -1);
        calendar2.add(Calendar.DATE, -1);
        Date yesterday = calendar.getTime();
        Date yesterday0 = calendar2.getTime();
        calendar.add(Calendar.DATE, -6);
        calendar2.add(Calendar.DATE, -6);
        Date oneWeekAgo = calendar.getTime();
        Date oneWeekAgo0 = calendar2.getTime();

        Map<String, Object> map = new HashMap<>();
        map.put("todayTotal", dataCenterMapper.total(id, now0, now, true, false, false));
        map.put("todayTotalAll", dataCenterMapper.total(id, now0, now, true, true, true));
        map.put("todayNewCustomer", dataCenterMapper.newCustomer(id, now0, now, true, true));
        map.put("todayBusinessWithOrders", dataCenterMapper.countBusinessWithOrders(id, now0, now));
        map.put("yesterdayTotal", dataCenterMapper.total(id, yesterday0, yesterday, true, false, false));
        map.put("yesterdayTotalAll", dataCenterMapper.total(id, yesterday0, yesterday, true, true, true));
        map.put("yesterdayNewCustomer", dataCenterMapper.newCustomer(id, yesterday0, yesterday, true, true));
        map.put("yesterdayBusinessWithOrders", dataCenterMapper.countBusinessWithOrders(id, yesterday0, yesterday));
        map.put("oneWeekAgoTotal", dataCenterMapper.total(id, oneWeekAgo0, oneWeekAgo, true, false, false));
        map.put("oneWeekAgoTotalAll", dataCenterMapper.total(id, oneWeekAgo0, oneWeekAgo, true, true, true));
        map.put("oneWeekAgoNewCustomer", dataCenterMapper.newCustomer(id, oneWeekAgo0, oneWeekAgo, true, true));
        map.put("oneWeekAgoBusinessWithOrders", dataCenterMapper.countBusinessWithOrders(id, oneWeekAgo0, oneWeekAgo));

        Date firstDayOfThisMonth = MyUtil.getMonthStartDate(now0);
        Date oneMonthAgo = MyUtil.getStartDay(MyUtil.addTimeSpan(-1, 0));
        Date firstDayOfLastMonth = MyUtil.getMonthStartDate(oneMonthAgo);

        if (!isFirstDayOfMonth) {
            map.put("thisMonthTotal", dataCenterMapper.total(id, firstDayOfThisMonth, now0, true, true, true));
            map.put("thisMonthOvertime", dataCenterMapper.overtime(id, firstDayOfThisMonth, now0, false));
            map.put("thisMonthNegativeComment", dataCenterMapper.negativeComment(id, firstDayOfThisMonth, now0, false));
            map.put("thisMonthCancel", dataCenterMapper.cancel(id, firstDayOfThisMonth, now0, false));
            map.put("lastMonthTotal", dataCenterMapper.total(id, firstDayOfLastMonth, oneMonthAgo, true, true, true));
            map.put("lastMonthOvertime", dataCenterMapper.overtime(id, firstDayOfLastMonth, oneMonthAgo, false));
            map.put("lastMonthNegativeComment", dataCenterMapper.negativeComment(id, firstDayOfLastMonth, oneMonthAgo, false));
            map.put("lastMonthCancel", dataCenterMapper.cancel(id, firstDayOfLastMonth, oneMonthAgo, false));
        }

        Calendar calendar1 = Calendar.getInstance();
        int hour = calendar1.get(Calendar.HOUR_OF_DAY);
        calendar1.set(Calendar.MINUTE, 0);
        calendar1.set(Calendar.SECOND, 0);
        Date end = calendar1.getTime();
        calendar1.add(Calendar.DATE, -1);
        calendar1.set(Calendar.HOUR_OF_DAY, 23);
        calendar1.set(Calendar.SECOND, 1);
        Date start = calendar1.getTime();
        map.put("todayHourlySum", dataCenterMapper.hourlySum(id, start, end));
        map.put("yesterdayHourlySum", dataCenterMapper.hourlySum(id, MyUtil.addTimeSpan(start, 0, -1), MyUtil.addTimeSpan(end, 0, -1)));
        map.put("oneWeekAgoHourlySum", dataCenterMapper.hourlySum(id, MyUtil.addTimeSpan(start, 0, -7), MyUtil.addTimeSpan(end, 0, -7)));
        map.put("currentHour", hour);
        return Result.ok("", map);
    }
}
